package com.SouthernWall_404.age_tool.common.world.block.TriggerBlocks;

import com.SouthernWall_404.age_tool.common.API.Capability.Provider.TriggersCapProvider;
import com.SouthernWall_404.age_tool.common.API.Capability.TriggersCap;
import com.SouthernWall_404.age_tool.common.Network.ModChannels;
import com.SouthernWall_404.age_tool.common.Network.Packets.C2S.HandShake.MimicBlockRequireHandShakePacket;
import com.SouthernWall_404.age_tool.common.init.ModBlockEntities;
import com.SouthernWall_404.age_tool.common.world.Entity.TriggerBlockEntity;
import com.SouthernWall_404.age_tool.common.world.block.AbstractMimicBlock;
import com.SouthernWall_404.age_tool.common.world.item.ExcuteBlockItem;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.world.level.block.HorizontalDirectionalBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.gameevent.GameEventListener;
import net.minecraft.world.phys.BlockHitResult;
import org.jetbrains.annotations.Nullable;

public class AbstractTriggerBlock extends AbstractMimicBlock implements EntityBlock {




    private int button_time=40;



    public AbstractTriggerBlock(Properties p_54120_) {
        super(p_54120_.strength(2.0f)
                .noOcclusion()
                .dynamicShape());
    }

    @Override
    public void onRemove(BlockState blockState, Level level, BlockPos blockPos, BlockState p_60518_, boolean p_60519_) {

        BlockEntity blockEntity=level.getBlockEntity(blockPos);
        if(blockEntity instanceof TriggerBlockEntity triggerBlockEntity)
        {
        TriggersCap triggersCap=triggerBlockEntity.getTriggers();
        if(triggersCap!=null)
        {
            triggersCap.removeRemote(triggerBlockEntity.remoteId);
        }
    }

        super.onRemove(blockState, level, blockPos, p_60518_, p_60519_);
    }




    public void triggered(Level level,BlockPos blockPos)

    {
        BlockEntity blockEntity = level.getBlockEntity(blockPos);
        if (blockEntity instanceof TriggerBlockEntity triggerBlockEntity) {


            TriggersCap triggersCap = level.getCapability(TriggersCapProvider.TRIGGERSCAP).orElse(null);

            if (triggersCap != null) {
//                player.sendSystemMessage(Component.translatable(""+triggersCap.allRemoteIdState));
                if(!level.isClientSide)
                {
                    if(!triggerBlockEntity.isPressed)
                    {
                        triggerBlockEntity.isPressed=true;
                    }
                }
            }
        }
    }



    @Override
    public InteractionResult use(BlockState blockState, Level level, BlockPos blockPos, Player player, InteractionHand hand, BlockHitResult blockPlaceContext) {

        InteractionResult interactionResult=super.use(blockState, level, blockPos, player, hand, blockPlaceContext);

        BlockEntity blockEntity = level.getBlockEntity(blockPos);
        if (blockEntity instanceof TriggerBlockEntity triggerBlockEntity) {


            TriggersCap triggersCap = level.getCapability(TriggersCapProvider.TRIGGERSCAP).orElse(null);

            if (triggersCap != null) {
                ItemStack itemStack = player.getItemInHand(hand);
                if (itemStack.getItem() instanceof ExcuteBlockItem excuteBlockItem) {
                    ExcuteBlockItem.setRemoteId(itemStack, triggerBlockEntity.remoteId);
//                    return InteractionResult.SUCCESS;
                }
            }
        }


        return interactionResult;
    }

    @Override
    public @Nullable <T extends BlockEntity> GameEventListener getListener(ServerLevel level, T blockEntity) {
        return super.getListener(level, blockEntity);
    }

    @Override
    public @Nullable <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState blockState, BlockEntityType<T> blockEntityType) {
        return (lvl, pos, st, blockEntity) -> {



            if (blockEntity instanceof TriggerBlockEntity triggerBlockEntity) {

                if(triggerBlockEntity.isPressed)
                {
                    triggerBlockEntity.isTriggered=true;
                    triggerBlockEntity.pressed_time++;
                    if(triggerBlockEntity.pressed_time>=button_time)
                    {
                        triggerBlockEntity.pressed_time=0;
                        triggerBlockEntity.isPressed=false;
                    }
                }
                else
                {
                    triggerBlockEntity.isTriggered=false;
                }

                TriggersCap triggersCap=triggerBlockEntity.getTriggers();

                if(triggersCap!=null)
                {
                    if(!triggersCap.allRemoteIdState.containsKey(triggerBlockEntity.remoteId))
                    {
                        triggersCap.addRemote(triggerBlockEntity.remoteId);
                    }
                    else
                    {
                        triggersCap.setAvailability(triggerBlockEntity.remoteId,triggerBlockEntity.isTriggered);
                    }

                }
//                ModChannels.sendToServer(new MimicBlockRequireHandShakePacket(pos));
//                super.getTicker(level,triggerBlockEntity.mimicBlock,blockEntityType);
            }

        };
    }

    @Override
    public @Nullable BlockEntity newBlockEntity(BlockPos blockPos, BlockState blockState) {
        TriggerBlockEntity triggerBlockEntity=new TriggerBlockEntity(ModBlockEntities.TRIGGER_BLOCK_ENTITY.get(),blockPos,blockState);
//        triggerBlockEntity.addUUID();
        return triggerBlockEntity;
    }
}
